#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define ldb long double
using namespace std;
char rdc[1<<20],*rS,*rT;
#define gc() (rS==rT?rT=(rS=rdc)+fread(rdc,1,1<<20,stdin),(rS==rT?EOF:*rS++):*rS++)
inline int read(){
	int x=0;char ch;
	while(!isdigit(ch=gc()));
	do x=(x<<1)+(x<<3)+(ch^48);while(isdigit(ch=gc()));
	return x;
}
const int N=2e5+5,M=1e6+5,Mod=998244353,inv2=(Mod+1)/2;
int n,m,rtx,rty,tim,ans;
int GH[N],GNXT[N],sz[N],a[N];
int HH[M],HNXT[M],s[M],c[M],d[M],t[M];
int L[M],R[M],siz[M],f[M],son[M],top[M],dep[M];
inline void fMod(int &x){
    x>=Mod?x-=Mod:0;
}
inline void add(int x,int y){
    (y<0)?(y+=Mod):0;
    for(int i=x,z=1LL*(x-1)*y%Mod;i<=m;i+=i&-i)   
        fMod(c[i]+=y),fMod(d[i]+=z);
}
inline LL sum(int x){
    int s1=0,s2=0;
    for(int i=x;i;i-=i&-i)
        fMod(s1+=c[i]),fMod(s2+=d[i]);
    return (1LL*x*s1-s2)%Mod;
}
inline void upd(int x,int y){
    for(;x;x=f[top[x]])add(L[top[x]],y),add(L[x]+1,-y);
}
inline int qry(int x){
    int res=0;
    for(;x;x=f[top[x]])
        res=(res+sum(L[x])-sum(L[top[x]]-1))%Mod;
    return res;
}
void dfs1(int x){
    sz[x]=1;
    for(int v=GH[x];v;v=GNXT[v])
        dfs1(v),sz[x]+=sz[v];
}
void dfs2(int x){
    ans=(ans+1LL*qry(a[x])*sz[x])%Mod;
    for(int v=GH[x];v;v=GNXT[v])
        upd(a[x],n-sz[x]-sz[v]),dfs2(v),upd(a[x],sz[x]+sz[v]-n);
}
void dfs3(int x,int fa){
    siz[x]=1,f[x]=fa,dep[x]=dep[fa]+1;
    for(int v=HH[x];v;v=HNXT[v]){
        dfs3(v,x),siz[x]+=siz[v],fMod(s[x]+=s[v]);
        if(siz[v]>siz[son[x]])son[x]=v;
    }
}
void dfs4(int x,int tp){
    L[x]=++tim,top[x]=tp;
    if(son[x])dfs4(son[x],tp);
    for(int v=HH[x];v;v=HNXT[v])if(v!=son[x])dfs4(v,v);
    fMod(t[L[x]]+=s[x]),fMod(t[R[x]=tim+1]+=Mod-s[x]);
}
signed main(){
//#define mytest
#ifndef mytest
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
#endif
	n=read(),m=read();
    for(int i=1;i<=n;++i){
        int x=read();
        if(x)GNXT[i]=GH[x],GH[x]=i;
        else rtx=i;
    }
    dfs1(rtx);
    for(int i=1;i<=m;++i){
        int x=read();
        if(x)HNXT[i]=HH[x],HH[x]=i;
        else rty=i;
    }
    for(int i=1;i<=n;++i)fMod(s[a[i]=read()]+=sz[i]);
    dfs3(rty,0),dfs4(rty,rty);
    for(int i=1;i<=m;++i)fMod(t[i]+=t[i-1]);
    for(int i=1;i<=n;++i)
        ans=(ans+1LL*(t[L[a[i]]]-1LL*dep[a[i]]*sz[i]%Mod)*sz[i])%Mod;
    ans=1LL*ans*inv2%Mod,dfs2(rtx);
    (ans<0)?(ans+=Mod):0,printf("%d\n",ans);
	return 0;
}
/*

*/